package urltest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class Test {
	public String getContent() throws Exception {

		URL url1 = new URL("http://1.85.55.152/sys/ValidateCode.aspx");
		HttpURLConnection con1 = (HttpURLConnection)url1.openConnection();
		
		con1.setRequestMethod("GET");
		con1.setRequestProperty("Accept",  "image/webp,image/*,*/*;q=0.8");
		con1.setRequestProperty("Connection", "keep-alive");
		con1.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
		con1.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
		con1.setRequestProperty("Host", "1.85.55.152");
		con1.setRequestProperty("Referer", "http://1.85.55.152/ZNPK/TeacherKBFB.aspx");
		con1.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
		con1.setDoInput(true);
		con1.setDoOutput(true);
		
		String session = con1.getHeaderField("Set-Cookie");
		String[] sessionid = session.split(";");
		
		BufferedInputStream in1 = new BufferedInputStream(con1.getInputStream());
		BufferedOutputStream out1 = new BufferedOutputStream(new FileOutputStream("checkCode.jpg"));
		byte[] buffer = new byte[128];
		int len = 0;
		while ((len = in1.read(buffer)) != -1) {
			out1.write(buffer, 0, len);
		}
		out1.close();
		in1.close();
		
		BufferedReader Sreader = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the techer id:");
		String id=Sreader.readLine();
		System.out.println("Enter the validation code:");
		String vc= Sreader.readLine();
		
		URL url2 = new URL("http://1.85.55.152/ZNPK/TeacherKBFB_rpt.aspx");
		HttpURLConnection con2 = (HttpURLConnection)url2.openConnection();

		con2.setRequestMethod("POST");
		con2.setRequestProperty("Cookie", sessionid[0]);
		con2.setRequestProperty("Accept",  "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		con2.setRequestProperty("Accept-Encoding", "gzip, deflate");
		con2.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
		con2.setRequestProperty("Cache-Control", "max-age=0");
		con2.setRequestProperty("Connection", "keep-alive");
		con2.setRequestProperty("Content-Length", "49");
		con2.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		con2.setRequestProperty("Host", "1.85.55.152");
		con2.setRequestProperty("Origin", "http://1.85.55.152");
		con2.setRequestProperty("Referer", "http://1.85.55.152/ZNPK/TeacherKBFB.aspx");
		con2.setRequestProperty("Upgrade-Insecure-Requests", "1");
		con2.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
		con2.setDoInput(true);
		con2.setDoOutput(true);
		
		OutputStream outTo = con2.getOutputStream();
		String data="Sel_XNXQ=20161&Sel_JS="+id+"&type=1&txt_yzm="+vc;
		outTo.write(data.getBytes());
		outTo.close();
		InputStream in2 = con2.getInputStream();
		String resStr = "";
		BufferedReader reader3 = new BufferedReader(new InputStreamReader(in2));
		String line = "";
		while ((line = reader3.readLine()) != null) {
			resStr += line;
		}
		File html = new File("test.html");
		OutputStream outres = new FileOutputStream(html);
		outres.write(resStr.getBytes());
		
		in2.close();
		
		return resStr;
		}
//	
//	public Map<String, String> getList() {
//		
//	}
	
	public static void main(String[] args) {
		
	}
}
